package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.example.entity.Cart;
import java.util.List;

public interface ICartMapper extends BaseMapper<Cart> {
    // 1. 根据用户编号查询所有购物车信息
    @Select("SELECT * FROM cart WHERE user_id = #{userId}")
    List<Cart> selectByUserId(@Param("userId") String userId);

    // 2. 根据用户编号和商家编号查询购物车信息
    @Select("SELECT * FROM cart WHERE user_id = #{userId} AND business_id = #{businessId}")
    List<Cart> selectByUserIdAndBusinessId(@Param("userId") String userId, @Param("businessId") Integer businessId);

    // 3. 向购物车表插入记录
    @Insert("INSERT INTO cart (user_id, business_id, food_id, quantity) VALUES (#{userId}, #{businessId}, #{foodId}, 1)")
    int insertCart(@Param("userId") String userId, @Param("businessId") Integer businessId, @Param("foodId") Integer foodId);

    // 4. 更新购物车中特定食品的数量
    @Update("UPDATE cart SET quantity = #{quantity} WHERE user_id = #{userId} AND business_id = #{businessId} AND food_id = #{foodId}")
    int updateCartQuantity(@Param("userId") String userId, @Param("businessId") Integer businessId, @Param("foodId") Integer foodId, @Param("quantity") Integer quantity);

    // 5. 删除购物车中特定食品
    @Delete("DELETE FROM cart WHERE user_id = #{userId} AND business_id = #{businessId} AND food_id = #{foodId}")
    int deleteByUserIdBusinessIdAndFoodId(@Param("userId") String userId, @Param("businessId") Integer businessId, @Param("foodId") Integer foodId);

    // 6. 删除购物车中某商家的所有食品
    @Delete("DELETE FROM cart WHERE user_id = #{userId} AND business_id = #{businessId}")
    int deleteByUserIdAndBusinessId(@Param("userId") String userId, @Param("businessId") Integer businessId);
} 